<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('usrModule.list.table.registeredTime')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.channelSource')">
          <el-select v-model="searchForm.channel" clearable :placeholder="$t('usrModule.list.prompt.channelSource')" @change="setSource">
            <el-option
              v-for="item in $t('usrModule.list.source')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.promotionMethod')">
          <el-select v-model="searchForm.promotionTypeStr" clearable multiple collapse-tags :placeholder="$t('usrModule.list.prompt.promotionMethod')">
            <el-option
              v-for="item in promotionData"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.referrer')">
          <el-input v-model="searchForm.referrerName" clearable :placeholder="$t('usrModule.list.prompt.referrer')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.packageNames')">
          <el-select v-model="searchForm.packageName" clearable :placeholder="$t('usrModule.list.prompt.packageNames')" style="width: 100%">
            <el-option
              v-for="item in allPackageData"
              :key="item.packageName"
              :label="item.packageName"
              :value="item.packageName"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.userLevel')">
          <el-select v-model="searchForm.userGradeList" multiple clearable collapse-tags :placeholder="$t('usrModule.list.prompt.userLevel')" style="width: 100%">
            <el-option
              v-for="item in userGradeData"
              :key="item.id"
              :label="item['userLevelName']"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.userId')">
          <el-input v-model="searchForm.id" clearable :placeholder="$t('usrModule.list.prompt.userId')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.international')">
          <el-input v-model="searchForm.areaCode" clearable :placeholder="$t('usrModule.list.prompt.international')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.mobile')">
          <el-input v-model="searchForm.phoneNumber" clearable :placeholder="$t('usrModule.list.prompt.mobile')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        class="Top"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          :label="tableText['userId']"
          min-width="150"
        />
        <el-table-column
          prop="areaCode"
          :label="tableText['international']"
          min-width="150"
        />
        <el-table-column
          prop="phoneNumber"
          :label="tableText['mobile']"
          min-width="150"
        />
        <el-table-column
          prop="userGrade"
          :label="tableText['userLevel']"
          min-width="150"
        />
        <el-table-column
          prop="registerTimeStr"
          :label="tableText['registeredTime']"
          min-width="170"
        />
        <el-table-column
          prop="packageName"
          :label="tableText['packageNames']"
          min-width="200"
        />
        <el-table-column
          prop="channelStr"
          :label="tableText['channelSource']"
          min-width="150"
        />
        <el-table-column
          prop="promotionTypeStr"
          :label="tableText['promotionMethod']"
          min-width="150"
        />
        <el-table-column
          prop="referrerName"
          :label="tableText['referrer']"
          min-width="150"
        />
        <el-table-column
          :label="tableText['creditStatus']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setState(scope.row['creditStatus'], 'credit') }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="tableText['accountStatus']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setState(scope.row.status, 'account') }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="150"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="info(scope.row)">{{ $t('common.btnGroup.i') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { getUserList, allPackage, getUserLevel } from '@/api/user'
import { formatDate, ObjToQueryString } from '@/utils/method'
export default {
  name: 'List',
  data() {
    return {
      loading: true,
      tableData: [],
      promotionData: [],
      allPackageData: [],
      userGradeData: [],
      time: [new Date(), new Date()],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('usrModule.list.table')
    this.getData()
    this.getAllPackage()
    this.getUserLevelData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        registerTimeStart: null,
        registerTimeEnd: null,
        channel: null,
        promotionTypeStr: null,
        referrerName: null,
        packageName: null,
        userGradeList: [],
        id: null,
        areaCode: null,
        phoneNumber: null
      }
    },
    getAllPackage() {
      allPackage()
        .then(res => {
          if (res.success) {
            this.allPackageData = res.data.filter(v => {
              return v.packageName
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getUserLevelData() {
      const data = {
        isPage: false
      }
      getUserLevel(data)
        .then(res => {
          if (res.success) {
            const arr = (res.data).filter(item => {
              return item['status'] === 1
            })
            arr.forEach(v => {
              this.searchForm.userGradeList.push(v.id)
            })
            this.userGradeData = arr
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    setState(state, data) {
      const map = this.$t('usrModule.list')[data]
      const name = map.filter(item => {
        return item.value === state
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = [new Date(), new Date()]
      this.userGradeData.forEach(v => {
        this.searchForm.userGradeList.push(v.id)
      })
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['registerTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['registerTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['registerTimeStart'] = null
        data['registerTimeEnd'] = null
      }
      return data
    },
    getData() {
      const data = this.searchData()
      this.loading = true
      getUserList(data)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    info(item) {
      const paramData = {
        from: 'user',
        userId: item.id
      }
      const para = ObjToQueryString(paramData).substring(-1)
      window.open(`${window.location.origin + window.location.pathname}#/usr/detail?${para}`, '_blank')
    },
    setSource() {
      const channel = this.searchForm.channel
      if (channel === 1) {
        this.promotionData = this.$t('usrModule.list.method')
      } else if (channel === 2) {
        this.promotionData = this.$t('usrModule.list.method1')
      } else if (channel === 3) {
        this.promotionData = []
        const arr = this.allPackageData.filter(v => {
          return v['mediaSource'] !== null
        })
        const node = []
        arr.forEach(v => {
          node.push({
            desc: v['mediaSource'],
            value: v['mediaSource']
          })
        })
        this.promotionData = node
      } else {
        this.promotionData = []
      }
      this.searchForm.promotionTypeStr = null
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
